package org.pillar.fitness.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.pillar.fitness.common.core.domain.AjaxResult;
import org.pillar.fitness.common.utils.SecurityUtils;
import org.pillar.fitness.course.domain.Course;
import org.pillar.fitness.course.domain.vo.CourseVo;
import org.pillar.fitness.course.mapper.CourseMapper;
import org.pillar.fitness.course.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author javaboy
 * @since 2023-01-10
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    CourseMapper courseMapper;

    @Override
    public List<CourseVo> selectCourseList(CourseVo courseVo) {
        return courseMapper.selectCourseList(courseVo);
    }

    @Override
    public AjaxResult addCourse(CourseVo courseVo) {
        Course course = new Course();
        BeanUtils.copyProperties(courseVo, course);
        course.setCreateBy(SecurityUtils.getUsername());
        course.setCreateTime(LocalDateTime.now());
        course.setDelFlag(0);
        return save(course) ? AjaxResult.success("添加成功") : AjaxResult.error("添加失败");
    }

    @Override
    public AjaxResult updateCourse(CourseVo courseVo) {
        Course course = new Course();
        BeanUtils.copyProperties(courseVo, course);
        course.setUpdateBy(SecurityUtils.getUsername());
        course.setUpdateTime(LocalDateTime.now());
        course.setCreateBy(null);
        course.setCreateTime(null);
        course.setDelFlag(null);
        return updateById(course) ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
    }

    @Override
    public boolean deleteCourseByIds(Long[] courseIds) {
        UpdateWrapper<Course> uw = new UpdateWrapper<>();
        uw.lambda().set(Course::getDelFlag,1).in(Course::getCourseId,courseIds);
        return update(uw);
    }

    @Override
    public boolean importCourse(List<CourseVo> coursesList, boolean updateSupport) {
        //updateSupport 判断是覆盖还是新增数据
        if (updateSupport){
            //想更新，根据id去更新
            //更新需要设置更新时间、更新的人
            List<Course> courses = coursesList.stream().map(c -> {
                Course course = new Course();
                BeanUtils.copyProperties(c, course);
                course.setUpdateBy(SecurityUtils.getUsername());
                course.setUpdateTime(LocalDateTime.now());
                return course;
            }).collect(Collectors.toList());
            //mybatis-plus提供了批量插入的方法
            return updateBatchById(courses);
        }else{
            //新增 插入
            //插入需要设置新增时间和创建人
            List<Course> courses = coursesList.stream().map(c -> {
                Course course = new Course();
                BeanUtils.copyProperties(c, course);
                course.setCreateBy(SecurityUtils.getUsername());
                course.setCreateTime(LocalDateTime.now());
                course.setCourseId(null);
                return course;
            }).collect(Collectors.toList());
            return saveBatch(courses);
        }
    }
}
